<template>
  <el-card>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>
        <a href="/home" onclick="return false" @click="back">首页</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>资料设置</el-breadcrumb-item>
      <el-breadcrumb-item>修改密码</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form label-width="100px">
      <el-form-item label="新密码：">
        <el-input v-model="form.newPassword" type="password" clearable show-password></el-input>
      </el-form-item>
      <el-form-item label="确认密码：">
        <el-input v-model="form.cfmPassword" type="password" clearable show-password></el-input>
      </el-form-item>
      <el-form-item style="margin-top: 50px">
        <el-button type="primary" @click="updatePassword">确认</el-button>
        <el-button @click="clear">重置</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="40%"
      top="25vh"
      center
      :close-on-press-escape="false"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <h1 class="dialogContent">您的密码已修改，请您重新登录。</h1>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClick">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  name: 'changepwd',
  data () {
    return {
      form: {
        newPassword: '',
        cfmPassword: ''
      },
      centerDialogVisible: false
    }
  },
  methods: {
    back () {
      this.$router.push('/home')
      this.$store.state.activePath = '/home'
    },
    clear () {
      this.form.newPassword = ''
      this.form.cfmPassword = ''
    },
    async updatePassword () {
      if (this.form.cfmPassword !== this.form.newPassword) return this.$message.error('您两次输入的密码不一致，请重新输入')
      const { data: res } = await this.$http.post('/updatePassword', {
        password: this.form.newPassword
      })
      if (res.status !== 200) return this.$message.error('修改密码失败！')
      this.centerDialogVisible = true
      this.$store.state.activePath = '/basicinfo'
    },
    handleClick () {
      this.$router.push('/')
      this.$store.state.activePath = '/home'
    }
  }
}
</script>

<style lang="less" scoped>
.el-form{
  margin-top: 50px;
  .el-input{
    width: 40%;
  }
}
  .dialogContent {
    font-size: 30px;
    margin-left: 120px;
  }
</style>
